package cn.itcast.web.controller.system;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/system/role", name = "角色管理")
public class RoleController extends BaseController {
    @Reference
    private RoleService roleService;
    @Reference
    private ModuleService moduleService;




    @RequestMapping(value = "/updateRoleModule", name = "实现角色分配权限")
    public String updateRoleModule(String roleId, String[] moduleIds){
        roleService.updateRoleModule(roleId,moduleIds);
        return "redirect:/system/role/roleModule.do?roleId=" + roleId;
    }


    @RequestMapping(value = "/getZtreeNodes", name = "回显模块树")
   @ResponseBody
   public List<Map> getZtreeNodes(String roleId) {
       List<Map> jsonList = new ArrayList<>();
       List<Module> moduleList = moduleService.findAll();
       List<String> moduleIds  = roleService.findModuleIdsByRoleId(roleId);
       for (Module module : moduleList) {
           Map node = new HashMap();
           node.put("id", module.getId());
           node.put("pId", module.getParentId());
           node.put("name", module.getName());
           if(moduleIds.contains(module.getId())){
               node.put("checked", true);
           }
           jsonList.add(node);
       }
       return jsonList;
   }



@RequestMapping(value = "/roleModule",name = "进入模块分配页面")
public String roleModule(String roleId){
    Role role = roleService.findById(roleId);
    request.setAttribute("role",role);
    return "system/role/role-module";
}


    @RequestMapping(value = "/list", name = "分页查询")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "3") Integer pageSize) {

        PageInfo page = roleService.findByPage(pageNum, pageSize, getCompanyId());
        request.setAttribute("page", page);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd", name = "新增页面")
    public String toAdd() {
        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit", name = "新增/修改")
    public String edit(Role role) {
        if (StrUtil.hasEmpty(role.getId())) {
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            roleService.save(role);
        } else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "修改页面")
    public String toUpdate(String id) {
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        return "system/role/role-update";
    }


    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }
}
